package com.qidian.buy.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.buy.entity.Item;
import com.qidian.buy.entity.ItemDesc;
import com.qidian.buy.product.mapper.ItemDescMapper;
import com.qidian.buy.product.mapper.ItemMapper;
import com.qidian.buy.product.service.IItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidian.buy.util.Constant;
import com.qidian.buy.util.pojo.BuyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.Wrapper;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2023-07-05
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public BuyResult saveItem(Item item) {
        // 查询id
        QueryWrapper wrapper = new QueryWrapper();
        // 按照id降序排序
        wrapper.orderByDesc("id");
        wrapper.last(" limit 1");
        List<Long> list = itemMapper.selectObjs(wrapper);
        Long id = list.get(0) + 1;
        // 补全数据
        item.setId(id);
        item.setStatus((byte) 1);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        // 保存item
        itemMapper.insert(item);
        // 保存itemDesc
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(id);
        itemDesc.setCreated(new Date());
        itemDesc.setUpdated(new Date());
        itemDesc.setItemDesc(item.getItemDesc());
        itemDescMapper.insert(itemDesc);
        return BuyResult.ok();
    }

    @Override
    public BuyResult queryItemAndDescById(Long itemId) {
        return BuyResult.ok(itemMapper.queryItemAndDescById(itemId));
    }

    @Override
    public BuyResult updateItem(Item item) {
        // 编辑商品
        // 调整数据
        item.setUpdated(new Date());
        itemMapper.updateById(item);
        // 编辑商品详情
        itemDescMapper.updateById(item.getDesc());
        return BuyResult.ok();
    }

    @Override
    public BuyResult deleteItem(Long itemId) {
        Item item = new Item();
        item.setId(itemId);
        item.setStatus((byte) Constant.DATA_STATUS_DISCARD);
        item.setUpdated(new Date());
        int result = itemMapper.updateById(item);
        System.out.println(result);
        return BuyResult.ok();
    }

    @Override
    public BuyResult queryByPage(int page, int limit) {
        IPage<Item> pager = new Page<>();
        pager.setCurrent(page);// 当前页码
        pager.setSize(limit);// 设置每页大小
        // 查询的结果依然使用pager接收
        pager =  itemMapper.selectPage(pager,null);// 暂时没有条件，wrapper传递null
        return BuyResult.ok(pager);
    }


}
